package com.shanzhe.eshop.common.jdk;

import lombok.SneakyThrows;

import java.util.concurrent.Exchanger;

/**
 * @Author yinwei
 * @create 2021/12/13
 * @Version 1.0
 */
public class ExchangeDemo {

    public static void main(String[] args) {
        final Exchanger<String> exchanger = new Exchanger<String>();

        new Thread() {
            @SneakyThrows
            @Override
            public void run() {
                String data = exchanger.exchange("线程1的数据");
                System.out.println("线程1获取到线程2交换过来的数据" + data);
            }
        }.start();

        new Thread() {
            @SneakyThrows
            @Override
            public void run() {
                String data = exchanger.exchange("线程2的数据");
                System.out.println("线程2获取到线程1交换过来的数据" + data);
            }
        }.start();

    }
}
